﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;

namespace BackEnd.Controllers
{
    public class ContentTypeController : Controller
    {
        //
        // GET: /ContentType/
        [CustomAuthorize(Action = Constants.AdminActions.ListContentType)]
        public ActionResult Index()
        {
            ContentTypeModel contentTypeModel = new ContentTypeModel();
            contentTypeModel.ListContentType = new ContentTypeBL().GetAll();
            return View(contentTypeModel);
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateContentType)]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateContentType)]
        public ActionResult Create(ContentTypeModel contentTypeModel)
        {
            if (string.IsNullOrWhiteSpace(contentTypeModel.ContentTypeName))
            {
                ModelState.AddModelError("ContentTypeName", "ContentTypeName is not null");
            }
            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new ContentTypeBL().Insert(new ContentType { ContentTypeName = contentTypeModel.ContentTypeName });
                return RedirectToAction("Index", "ContentType");
            }
        }

        [CustomAuthorize(Action = Constants.AdminActions.EditContentType)]
        public ActionResult Edit(int id)
        {
            ContentTypeModel contentTypeModel = new ContentTypeModel();
            ContentType contentType=new ContentTypeBL().GetById(id);
            if (contentType != null)
            {
                contentTypeModel.Id = contentType.Id;
                contentTypeModel.ContentTypeName = contentType.ContentTypeName;
            }
            return View(contentTypeModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.EditContentType)]
        public ActionResult Edit(ContentTypeModel contentTypeModel)
        {
            if (string.IsNullOrWhiteSpace(contentTypeModel.ContentTypeName))
            {
                ModelState.AddModelError("ContentTypeName", "ContentTypeName is not null");
            }
            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new ContentTypeBL().Update(new ContentType {Id=contentTypeModel.Id, ContentTypeName = contentTypeModel.ContentTypeName });
                return RedirectToAction("Index", "ContentType");
            }
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteContentType)]
        public JsonResult Delete(string listId)
        {            
            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new ContentTypeBL().Delete(Convert.ToInt32(arrayId[i]));
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }
    }
}
